
#ifndef LanBSwizzle_h
#define LanBSwizzle_h

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

/**
 方法交换、替换
*/
NS_INLINE void lanB_swizzling(SEL originSel, Method originMethod, SEL swizzleSel, Method swizzleMethod, Class clazz) {
    BOOL didAddMethod = class_addMethod(clazz, originSel, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod));
    if (didAddMethod) {
        class_replaceMethod(clazz, swizzleSel, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    } else {
        method_exchangeImplementations(originMethod, swizzleMethod);
    }
}

/**
 hook实例方法
*/
NS_INLINE void lanB_swizzle_instanceMethod(SEL originSel, SEL swizzleSel, Class clazz) {
    Method originMethod = class_getInstanceMethod(clazz, originSel);
    Method swizzleMethod = class_getInstanceMethod(clazz, swizzleSel);
    lanB_swizzling(originSel, originMethod, swizzleSel, swizzleMethod, clazz);
}

/**
 hook类方法
*/
NS_INLINE void lanB_swizzle_classMethod(SEL originSel, SEL swizzleSel, Class clazz) {
    Method originMethod = class_getClassMethod(clazz, originSel);
    Method swizzleMethod = class_getClassMethod(clazz, swizzleSel);
    lanB_swizzling(originSel, originMethod, swizzleSel, swizzleMethod, object_getClass(clazz));
}

NS_INLINE BOOL lanB_IMP_isEqual(SEL selectorA, SEL selectorB, Class clazz) {
    IMP aIMP = class_getMethodImplementation(clazz, selectorA);
    IMP bIMP = class_getMethodImplementation(clazz, selectorB);
    return aIMP == bIMP;
}
//lanB_IMP_isEqual

#endif /* LanBSwizzle_h */

